import * as types from './mutationTypes';
import util from '@/lib/util';

const mutations = {
  [types.FETCH_FOLDER_LIST](state, data) {
    state.folderList = data;
  },
  [types.INSERT_FOLDER](state, data) {
    // 要判断返回的值判断是否成功
    state.folderList.push(data);
  },
  [types.FETCH_ARTICLE_LIST](state, data) {

    let tempList = [];
    state.articleList.forEach((item, index) => {
      tempList.push(item);
    });
    //给每个文章对象增加一个是否激活状态

    let list = data || [];
    list.forEach((item, index) => {
      item["_isActive"] = false;
      item["_createTimeFmt"] = util.formatTime(item.createTime);
      item["_updateTimeFmt"] = util.formatTime(item.updateTime);
      tempList.push(item);
    });

    state.articleList = tempList;
    // state.articleList = state.articleList.concat(data);
  },
  [types.FETCH_ARTICLE_DETAIL](state, data) {
    state.articleDetail = data;
    // 及时更新预览的值
    // localStorage.setItem('editorContent', state.articleDetail.origContent || '')
    // localStorage.setItem('title', state.articleDetail.title || '')
  },
  [types.INSERT_ARTICLE](state, data) {
    // 此时不调接口. 在内存文章列表中插入一篇文章对象，点保存文章的时候才调接口
    state.articleList.unshift({title: '请输入文章标题', content: '请输入文章内容', createTime: '', articleId: ''});

  },
  [types.CHANGE_FOLDER_ID](state, data) {
    // 修改folderID

    if (state.folderId == data) {
      if (data === -100 && state.articleList.length === 0) {

        state.beginEditorButton = true;
      }
    }

    state.folderId = data;
  },
  [types.JUDGE_LOADMORE](state, data) {
    // 修改folder
    state.isloadMore = data;
  },
  [types.SHOW_BEGIN_EDITOR_BUTTON](state, flag) {

    state.beginEditorButton = flag;
  },
  [types.CLEAR_ARTICLE_LIST](state, data) {
    // 清空文件夹下所有文章
    state.articleList = [];
  },
  [types.CLEAR_ARTICLE](state, data) {
    // 清空当前文章详情
    state.articleDetail = {};
    state.id = '';
  },
  [types.CHANGE_ARTICLE_ID](state, data) {
    // 获取当前文章的文章id
    state.id = data;
  },
  [types.SHOW_PREVIEW_LAYER](state, data) {
    state.showPreviewLayer = data;
  },
  [types.SHOW_CHOOSE_LAYER](state, data) {
    state.showChooseImg = data;
  },
  [types.CHOOSE_ARTICLE_IMG](state, data) {
    state.chooseArticleImg = data;
  },
  [types.CORPPER_IS_SHOW](state, data) {
    state.corpperIsShow = data;
  },
  [types.SHOW_SET_COVER_LAYER](state, data) {
    state.showChooseImg = data;
  },
  [types.CLEAR_FOLDER](state, index) {
    // 同时请求下一篇文章
    let nextIndex = index - 1;  // 删除最后一项 后为前一项
    let lastIndex = index + 1  // 删除不是最后一项 后为下一项
    if (state.folderList[lastIndex]) {
      if (state.folderList[lastIndex].folderId) {
        state.folderId = state.folderList[lastIndex].folderId;
      }
    } else if (state.folderList[nextIndex]) {
      if (state.folderList[nextIndex].folderId) {
        state.folderId = state.folderList[nextIndex].folderId;
      }
    }
    // 更新folderList列表, 同时清空掉articlelist及文章
    state.folderList.splice(index, 1)
  },
  [types.DELETE_ARTICLE](state, article_id) {


    let newArticleList = [];
    let delArticle = {folderId: -999};
    state.articleList.forEach((item, index) => {
      if (item.articleId == article_id) {
        //这里使用一个新的数组代替原来的，是为了使articleList监控生效
        delArticle = item;
      } else {
        newArticleList.push(item);
      }
    });
    state.articleList = newArticleList;
    //对目录列表中对应的目录中的文章数量做-1更新
    state.folderList.forEach((item, index) => {
      if (item.folderId == delArticle.folderId) {
        if (item["count"] > 0) {
          item["count"] = item["count"] - 1;
        }
      }
    });
  },
  [types.CLEAR_CHOOSE_ARTICLE_IMG](state, index) {
    state.chooseArticleImg = {}
  },
  [types.IS_FROM_ADD_IMG](state, flag) {
    state.isfromAddimg = flag
  },
  [types.CHANGE_FOLDER_NAME](state, param) {
    var name = param.name;
    var index = param.index;
    state.folderList[index].folderName = name;
  },
  [types.LOGING_CODE](state, param) {

    state.isLogin = param;
  },
  [types.UPDATE_ACTICLE_LIST](state, param) {
    // 更新文章list
    state.articleList.forEach((item, index) => {
      if (item.articleId == param.articleId) {
        item.title = param.title;
        item.origContent = param.origContent;
        item.content = param.content;
        item.updateTime = param.updateTime;
        item._updateTimeFmt = util.formatTime(item.updateTime);
        return;
      }
    });
  },
  [types.SEARCH_STATE](state, param) {
    //修改搜索状态数据
    for (let p in param) {
      if (param.hasOwnProperty(p)) {
        state.searchState[p] = param[p];
      }
    }
  },
  [types.ACTIVE_ARTICLE](state, param) {
    //激活某篇文章
    state.articleList.forEach((item, index) => {
      item["_isActive"] = item.articleId == param;

    });
  },
  [types.CHANGE_FOLDER_COUNT](state, count) {
    state.folderList.forEach((item, index) => {
      if (item.folderId == state.folderId) {
        item["count"] = count;
      }
    });
  },
  [types.SET_PREVIEW_INFO](state, info) {
    // 设置预览数据及更新
    state.previewInfo = Object.assign({}, state.previewInfo, info);

  },
  [types.NEW_ARTICLE](state, data) {
    //新建文章更新articleList
    let newArticleList = [];
    newArticleList.push(data);
    state.articleList.forEach((item, index) => {
      newArticleList.push(item);
    });
    state.articleList = newArticleList;
    //对目录列表中对应的目录中的文章数量做+1更新

    state.folderList.forEach((item, index) => {
      if (item.folderId == data.folderId) {
        item["count"] = item["count"] + 1;
      }
    });

  }

};

export default mutations;
